import dt from '@dt/dt'
import Opt from '@/components/dt/info/Opt.js'
import ruleOpt from '@/biz/option/rule.js'
import toolMgr from '@/biz/base/tool.js'

class CompanyOpt extends Opt {
  constructor(info) {
    super(info)
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let files = []
    if (this.info.invoiceReceiptAttaches && this.info.invoiceReceiptAttaches.length > 0) {
      this.info.invoiceReceiptAttaches.forEach((item) => {
        if (item) {
          files.push({
            name: item.fileName,
            url: item.attachAddress
          })
        }
      })
    }
    let file = {
      title: '',
      key: 'invoiceReceiptAttaches',
      upload: {
        title: '上传文件',
        api: toolMgr.unloadFile(),
        files,
        multiple: true
      },
      visible: !this.info.isInvoiceDelayUpload,
      data: {
        to: (v) => {
          let invoiceReceiptAttaches = v.map((item) => {
            return {
              fileName: item.name,
              attachAddress: item.response ? item.response.data.imageURL : item.url
            }
          })
          return {
            invoiceReceiptAttaches
          }
        }
      },
      rules: [
        {
          required: true,
          message: '请上传发票回执',
          type: 'array',
          trigger: 'blur'
        }
      ]
    }
    let delay = {
      title: '延期时间',
      key: 'delayTime',
      visible: !!this.info.isInvoiceDelayUpload,
      select: {
        key: 'delayTime'
      },
      rules: [ruleOpt.required('请选择延期时间')]
    }
    console.log(delay)
    this.all = [
      {
        title: '所属组织',
        key: 'companyName',
        text: {}
      },
      {
        title: '案场名称',
        key: 'housesName',
        text: {}
      },
      {
        title: '佣金单名称',
        key: 'commissionName',
        text: {}
      },
      {
        title: '结算月份',
        key: 'commissionMonth',
        text: {}
      },
      {
        title: '发票回执',
        key: 'isInvoiceDelayUpload',
        switch: {
          size: 'large',
          openText: '上传',
          closeText: '后补'
        },
        data: {
          from: (info) => !info.isInvoiceDelayUpload,
          to: (val) => {
            return { isInvoiceDelayUpload: !val }
          }
        },
        change: (res) => {
          delay.visible = !res.value
          file.visible = res.value
        }
      },
      file,
      delay
    ]
  }
}

export default CompanyOpt
